using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
using UnityEngine.Rendering.Universal.Internal;

public class CameraHeightRenderer : ScriptableRenderer
{
    private DepthOnlyPass m_depthPrepass;
    private RTHandle m_depthTexture;

    public CameraHeightRenderer(CameraHeightRendererData data) : base(data)
    {
        m_depthPrepass = new DepthOnlyPass(RenderPassEvent.BeforeRenderingPrePasses, RenderQueueRange.opaque, data.OpaqueLayerMask);
    }

    public override void Setup(ScriptableRenderContext context, ref RenderingData renderingData)
    {
        ref CameraData cameraData = ref renderingData.cameraData;
        Camera camera = cameraData.camera;
        RenderTextureDescriptor cameraTargetDescriptor = cameraData.cameraTargetDescriptor;
        
        RenderingUtils.ReAllocateIfNeeded(ref m_depthTexture, cameraTargetDescriptor, FilterMode.Point, wrapMode: TextureWrapMode.Clamp, name: "_CameraDepthTexture");
        
        m_depthPrepass.Setup(cameraTargetDescriptor, m_depthTexture);
        EnqueuePass(m_depthPrepass);

        for (int i = 0; i < rendererFeatures.Count; i++)
        {
            if (rendererFeatures[i].isActive)
                rendererFeatures[i].AddRenderPasses(this, ref renderingData);
        }
    }
}
